<?xml version="1.0" encoding="UTF-8"?><d:tdl xmlns="http://www.w3.org/1999/xhtml" xmlns:b="http://www.backbase.com/2006/btl"  xmlns:d="http://www.backbase.com/2006/tdl" >

	<d:namespace name="http://www.backbase.com/2006/btl">

		<d:uses element="element" src="../element/element.xml"/>
		<d:uses element="positionElement dimensionElement visualElement" src="../visualElement/visualElement.xml"/>
		<d:uses element="disableElement" src="../disableElement/disableElement.xml"/>
		<d:uses element="iconElement" src="../iconElement/iconElement.xml"/>
		<d:uses element="labelImplementor" src="../label/label.xml"/>

		<d:element name="menuBase" extends="b:element" abstract="true">
			

			<d:property name="selectedMenuItem">
				
				<d:setter type="text/javascript"><![CDATA[
					if (value) {
						if (bb.instanceOf(value, 'http://www.backbase.com/2006/btl', 'menuItemBase') && value.getProperty('parentNode') == this) {
							value.setProperty('selected', true);
						}
					}
				]]></d:setter>
				<d:getter type="text/javascript"><![CDATA[
					var oSelectedMenuItem = null;
					var aMenuItems = this.getProperty('menuItems');
					for (var i = 0, iMax = aMenuItems.length; i < iMax; i++) {
						if (aMenuItems[i].getProperty('selected')) {
							oSelectedMenuItem = aMenuItems[i];
							break;
						}
					}
					return oSelectedMenuItem;
				]]></d:getter>
			</d:property>

			<d:property name="menuItems">
				
				<d:getter type="text/javascript"><![CDATA[
					var aMenuItems = [];
					var aChildNodes = this.getProperty('childNodes');
					for (var i = 0, iMax = aChildNodes.length; i < iMax; i++) {
						if (bb.instanceOf(aChildNodes[i], 'http://www.backbase.com/2006/btl', 'menuItemBase')) {
							aMenuItems[aMenuItems.length] = aChildNodes[i];
						}
					}
					return aMenuItems;
				]]></d:getter>
			</d:property>

			<d:method name="handleSelectedMenuItem">
				
				<d:argument name="selectedMenuItem">
					
				</d:argument>
				<d:body type="text/javascript"><![CDATA[
					for (var i = 0, aMenuItems = this.getProperty('menuItems'), iMax = aMenuItems.length; i < iMax; i++) {
						var oMenuItem = aMenuItems[i];
						if (oMenuItem != selectedMenuItem) {
							oMenuItem.setProperty('selected', false);
						}
					}
				]]></d:body>
			</d:method>

			<!-- Whenever a child menu item is selected, the others should be deselected. -->
			<d:handler event="select" type="text/javascript"><![CDATA[
				var oTarget = event.target;
				var oCurrentTarget = event.currentTarget;
				if (oTarget.getProperty('parentNode') == oCurrentTarget) {
					this.handleSelectedMenuItem(oTarget);
				}
			]]></d:handler>
		</d:element>

		<d:element name="menuBarBase" extends="b:positionElement b:dimensionElement b:menuBase" abstract="true">
			
		</d:element>

		<d:element name="menuSeparatorBase" extends="b:element" abstract="true">
			
		</d:element>

		<d:element name="menuPopUpBase" extends="b:menuBase" abstract="true">
			

			

			

			<d:property name="open">
				
				<d:getter type="text/javascript"><![CDATA[
					return Boolean(this._._open);
				]]></d:getter>
			</d:property>

			<d:method name="open">
				
				<d:argument name="destination"/>
				<d:argument name="mode"/>
				<d:argument name="left"/>
				<d:argument name="top"/>
				<d:body type="text/javascript"><![CDATA[
					if (!this._._open) {
						if (destination) {
							bb.html.position(this.viewNode, destination.viewNode, mode, left, top);
						} else {
							mode = 'at-pointer';
							bb.html.position(this.viewNode, null, mode, left, top);
						}

						this._._open = true;
						bb.command.fireEvent(this, 'open', false, false);
					}
				]]></d:body>
			</d:method>

			<d:method name="close">
				
				<d:body type="text/javascript"><![CDATA[
					if (this._._open) {
						this._._open = false;

						var oSelectedMenuItem = this.getProperty('selectedMenuItem');

						if (oSelectedMenuItem) {
							oSelectedMenuItem.setProperty('selected', false);

							var oMenuPopUp = oSelectedMenuItem.getProperty('menuPopUp');
							if (oMenuPopUp) {
								oMenuPopUp.close();
							}
						}

						bb.command.fireEvent(this, 'close', false, false);
					}
				]]></d:body>
			</d:method>
		</d:element>

		<d:element name="menuItemBase" extends="b:iconElement b:disableElement b:labelImplementor" abstract="true">
			

			

			

			<d:property name="selected">
				
				<d:getter type="text/javascript"><![CDATA[
					return Boolean(this._._selected);
				]]></d:getter>
				<d:setter type="text/javascript"><![CDATA[
					value = Boolean(value);

					// Only do something if the value is changed
					if (this._._selected != value) {
						this._._selected = value;

						// Dispatch select or deselect event
						var sEvent = value ? 'select' : 'deselect';
						bb.command.fireEvent(this, sEvent, true, false);
					}
				]]></d:setter>
			</d:property>

			<d:property name="menuPopUp">
				
				<d:getter type="text/javascript"><![CDATA[
					var oMenuPopUp = null;
					var aChildNodes = this.getProperty('childNodes');
					for (var i = 0, iMax = aChildNodes.length; iMax > i; i++ ){
						if (bb.instanceOf(aChildNodes[i], 'http://www.backbase.com/2006/btl', 'menuPopUpBase')) {
							oMenuPopUp = aChildNodes[i];
							break;
						}
					}
					return oMenuPopUp;
				]]></d:getter>
			</d:property>

 			<d:property name="disabled">
				<!-- Reimplementation needed due to multiple inheritance overwriting
				the implementation of this property when extending from labelImplementor -->
				<d:setter type="text/javascript"><![CDATA[
					if (value) {
						this.setAttribute('disabled', 'disabled');
					} else {
						this.removeAttribute('disabled');
					}
				]]></d:setter>
				<d:getter type="text/javascript"><![CDATA[
					return btl.isTrueValue('disabled', this.modelNode.getAttribute('disabled'));
				]]></d:getter>
			</d:property>

			<d:property name="selectable">
				
				<d:getter type="text/javascript"><![CDATA[
					return this.viewNode.clientHeight > 0 && !this.getProperty('disabled');
				]]></d:getter>
			</d:property>

			<!-- Whenever a menu item is deselected, its menuPopUp should be closed. -->
			<d:handler event="deselect" type="text/javascript"><![CDATA[
				var oTarget = event.target;
				if (oTarget == this) {
					var oMenuPopUp = this.getProperty('menuPopUp');
					if (oMenuPopUp) {
						oMenuPopUp.close();
					}
				}
			]]></d:handler>
		</d:element>

		<d:element name="contextMenu" extends="b:element">
			

			<d:resource type="text/javascript"><![CDATA[
				btl.contextMenu = {};

				btl.contextMenu.handleContextMenu = function btl_menu_handleContextMenu(oEvent){
					var oTarget = oEvent.target;
					var oCurrentTarget = oEvent.currentTarget;
					var oCurrentActiveMenu = btl.menuBar.activeMenuBar;

					// if a menuBar is active it should be deactivated
					if (oCurrentActiveMenu && !btl.controller.containsElement(oCurrentActiveMenu, oTarget)) {
						oCurrentActiveMenu.setProperty('active', false);
					}

					if (oCurrentTarget._.__contextMenu && !oEvent.defaultPrevented){
						var oMenuPopUp = oCurrentTarget._.__contextMenu.getProperty('menuPopUp');

						if (oMenuPopUp){
							oMenuPopUp.open();
							// prevent that other context menus are shown
							oEvent.stopPropagation();
							oEvent.preventDefault();
						}
					}
				};
			]]></d:resource>

			<!-- added so that the children of the context menu can actually be appended to something -->
			<d:template type="application/xhtml+xml">
				<span style="display:none;"><d:content/></span>
			</d:template>

			<d:attribute name="menuPopUp">
				
			</d:attribute>

			<d:property name="menuPopUp">
				
				<d:getter type="text/javascript"><![CDATA[
					var sMenuPopUp = this.getAttribute('menuPopUp');
					var oMenuPopUp = null;

					if (sMenuPopUp){ // This is a reference to a popup defined elsewhere
						oMenuPopUp = bb.evaluateSmart(sMenuPopUp, this, this);
					} else {
						var aChildren = this.getProperty('childNodes');
						if (aChildren.length){
							for (var i = 0, iMax = aChildren.length; iMax > i; i++ ){
								if (bb.instanceOf(aChildren[i], 'http://www.backbase.com/2006/btl', 'menuPopUpBase')){
									oMenuPopUp = aChildren[i];
									break;
								}
							}
						}
					}
					return oMenuPopUp;
				]]></d:getter>
			</d:property>

			<!-- Clean up event listeners on the parent node when the contextMenu is removed. -->
			<d:handler event="DOMNodeRemovedFromDocument" type="text/javascript"><![CDATA[
				var oParent =  this.getProperty('parentNode');
				oParent.removeEventListener('contextmenu', btl.contextMenu.handleContextMenu, false);
				oParent._.__contextMenu = null;
			]]></d:handler>

			<!-- Add a contextmenu event handler to the parent node. -->
			<d:handler event="DOMNodeInsertedIntoDocument" type="text/javascript"><![CDATA[
				var oMenuPopUp = this.getProperty('menuPopUp');
				if (oMenuPopUp){
					var oParent =  this.getProperty('parentNode');
					oParent._.__contextMenu = this;
					oParent.addEventListener('contextmenu', btl.contextMenu.handleContextMenu, false);
				}
			]]></d:handler>
		</d:element>
	</d:namespace>
</d:tdl>